<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Cache;

class Address extends Base
{
    protected $name = 'user_address';

    public function p()
    {
        return $this->hasOne(Area::class, 'id', 'province')->field('id,name as title');
    }

    public function c()
    {
        return $this->hasOne(Area::class, 'id', 'city')->field('id,ext_name as title');
    }

    public function d()
    {
        return $this->hasOne(Area::class, 'id', 'district')->field('id,ext_name as title');
    }

    public function t()
    {
        return $this->hasOne(Area::class, 'id', 'town')->field('id,ext_name as title');
    }


    public static function add($param, $user): array
    {
        try {
            if (isset($param['default']) && $param['default'] == 1) {
                parent::update(['default' => 0], ['uid' => $user->id]);
            } else {
                $param['default'] = 0;
            }
            $keys = ['province', 'city', 'district', 'town'];
            $area = explode(',', $param['area']);
            foreach ($area as $key => $item) {
                $param[$keys[$key]] = $item;
            }
            unset($param['area']);
            if (isset($param['id']) && $param['id']) {
                parent::update($param);
                $msg = '已修改';
            } else {
                $param['uid'] = $user->id;
                parent::create($param);
                $msg = '已添加';
            }
            $url = !empty($param['type']) ? url('address/index')->build() . '?type=choose' : '';
            return ['code' => 1, 'msg' => $msg, 'url' => $url];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function edit($id, $user): array
    {
        try {
            $info = parent::where('id', $id)->find();
            if (empty($info) || $info->uid != $user->id) {
                return ['code' => -1, 'msg' => '加载失败，记录不存在'];
            }
            parent::destroy(['id' => $info->id]);
            return ['code' => 1, 'msg' => '已删除'];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function getDefault($uid)
    {
        try {
            $cacheId = Cache::get('choiceAddress_' . $uid);

            if ($cacheId) {
                $info = parent::where(['id' => $cacheId])->with(['p', 'c', 'd', 't'])
                    ->withoutField('is_del,create_time,update_time')->find();
                if ($info) return $info;
            }
            $info = parent::where(['uid' => $uid, 'default' => 1])->with(['p', 'c', 'd', 't'])
                ->withoutField('is_del,create_time,update_time')->find();
            if ($info) return $info;

            $info = parent::where(['uid' => $uid,])->with(['p', 'c', 'd', 't'])
                ->withoutField('is_del,create_time,update_time')->order('update_time', 'desc')->find();
            if ($info) return $info;

            return '';
        } catch (\Exception $e) {
            return '';
        }
    }

    public static function choice($id, $user): array
    {
        Cache::set('choiceAddress_' . $user->id, $id);
        return ['code' => 1, 'msg' => ''];
    }

}
